#coding=utf-8

#simple script to re-format a text grab from the pathfinder website into a CSV file understandable by mysql

import sys
import re
import string

classname = sys.argv[1]

line = "nothing_yet";

while not (line == ""):
    line = sys.stdin.readline()
    #print line
    
    #chrome separates table cells by one tab each
    columns = re.split('\t', line)
    
    #used to store each column for outputting later..
    parts = []
    
    #constant column numbers
    LEVEL_COL = 1
    ATTACKBONUS_COL = 2
    SAVES_COL = 3
    SPELLS_COL = 7
    
    #iterate through columns, add outer quotemarks and do column-specific formatting
    column_counter = 1
    for column in columns:    
        data = column
        
        #remove 1st, 2nd etc from level
        if column_counter == LEVEL_COL:
            data = re.sub("st|nd|th|rd", "", data)
        
        #attackbonus: we only care about the first number, the others can be calculated from total base attack bonus later
        if column_counter == ATTACKBONUS_COL:
            #find first / and ignore everything after it
            data = re.split("/", data)[0]
            #and remove the +
            data = data.replace("+", "")
            
        #save bonuses: remove + signs
        if column_counter >= SAVES_COL and column_counter < (SAVES_COL + 3):
            data = data.replace("+", "")
        
        #data inside columns should never contain line breaks
        data = data.replace("\n", "")
        
        #and all column data is surrounded by "double quotes"  
        parts.append("\"" + data + "\"")

        column_counter = column_counter + 1
    
    #separate columns with commas
    out_line = string.join(parts, ',')
    #remove any line breaks that have survived so far
    out_line = out_line.rstrip('\n')
    
    #don't print blank lines, it only confuses mysql
    if len(out_line) > 0:
        out_line = "\"" +classname +"\"," + out_line
        
        #mysql doesn't like uneven column counts so pad the whole thing out (messy!)
        DATA_COLUMNS = 16 #17 columns: 16 data + class name
        while column_counter <= DATA_COLUMNS:
                out_line +=",\"\""
                column_counter = column_counter + 1
        
        #output on stdout
        print out_line